package com.newtribe.access;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.newtribe.access.context.PeerContext;


/**
 * 
 */

/**
 * @author newtribe
 *
 */
public class BridgeProcessor extends Processor{
	


	  public BridgeProcessor(Server parent, int seq) {
	    super(parent, seq);
	  }
	  /**
	   * logger instance .
	   */
	  public static Log log =LogFactory.getLog(BridgeProcessor.class) ;
	  
	  /**
	   * server state information  instance
	   */
	 
	  /**
	   * init out runtime environment .
	   *
	   */
	  public void init() {

	  }
	  


	  /**
	   * when accept , process a socket .
	   * @param socket
	   * @throws java.lang.Exception
	   */
	  public void process(Socket socket ) throws Exception {
		  
		  // user login authentication
		  //recognize a request or response socket .
		  //

	    try {
			BridgeLayer layer =new BridgeLayer(socket) ;
			layer.init();

			String s =layer.getAuthInfo();
			if (s != null) {
			  String[] auth_u=s.split(";");
			  String user = auth_u[0] ;
			  layer.sendAuthResponse();
			  System.out.println("===============user connecting .. :"+user);
			  if (user !=null) {
				  PeerContextFactory factory =PeerContextFactory.getFactory();
				  PeerContext peerContext = factory.getContext(user);
				  peerContext.setRdpResponseSocket(socket);
				  peerContext.start();
				  return ;
			  }
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   
	    socket.close();
 		 
// 		 fromout.write(3);
// 		 fromout.flush();
 		  
// 		if (Arrays.equals(Constants.RESPONSE_HEAD,header)) {
// 			//this is a response agent connection 
// 			peerContext.setRdpResponseSocket(socket);
// 		}else {
// 			//this is a rdp request 
// 			
// 			peerContext.setRdpRequestSocket(socket);
// 		}
 		  
 		  
//	 		peerContext.setRdpRequestSocket(socket);
//
//		    Socket   rdpSocket=new Socket("211.88.9.173",3389);
//
// 	 		peerContext.setRdpResponseSocket(rdpSocket);
// 	 
 	 		
	    
// 		if (peerContext.getStatus() ==3) {
// 			//client and server have been ready .
// 			//assign a new task to 
// 			//execute queue .
// 			peerContext.start();
// 		}
 
	    }




	  public static void main(String[] args) {
	  }

}